package com.smarttourism.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.smarttourism.common.core.domain.R;
import com.smarttourism.domain.Goods;
import com.smarttourism.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author：王祖威
 * @Date ：2024/10/23 11:39
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    @GetMapping("/{id}")
    @PreAuthorize("@ss.hasPermi('goods:goods:list')")
    public R findbyid(@PathVariable("id") Integer id){
        Goods goods = goodsService.findById(id);
        return goods != null ?R.ok(goods):R.fail("新增失败");
    }

    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermi('goods:goods:list')")
    public R findAll(String gName,String typeName,int pageNum,int pageSize){
        PageHelper.startPage(pageNum,pageSize);

        List<Goods> list = goodsService.findAll(gName,typeName);
        PageInfo<Goods> goodsPageInfo = new PageInfo<>(list);
        return goodsPageInfo != null?R.ok(goodsPageInfo):R.fail("查询失败");
    }

    @PostMapping("/add")
    @PreAuthorize("@ss.hasPermi('goods:goods:list')")
    public R add(@RequestBody Goods goods){
        int i = goodsService.add(goods);
        return i>0?R.ok("新增成功"):R.fail("新增失败");

    }
    @DeleteMapping
    @PreAuthorize("@ss.hasPermi('goods:goods:list')")
    public R deleteBatch(Integer[] ids){
        int i = goodsService.removeBatch(ids);
        return i >0 ? R.ok("批量删除成功") : R.fail("批量删除失败");
    }
    @PutMapping("/update")
    @PreAuthorize("@ss.hasPermi('goods:goods:list')")
    public R update(@RequestBody Goods goods){
        int i = goodsService.updateById(goods);
        return i>0?R.ok("修改成功"):R.fail("修改失败");

    }

}

